const app = getApp()

var 
  companyCode = '',
  realName = '',
  phoneNum = '',
  companyName = '',
  companyAddress = '',
  companyPosition = '',
  storeId = '',
  email = '',
  changCompanyCode = false;

Page({

  /**
   * 页面
   * 的初始数据
   */
  data: {
    user_avater: "",
    user_nick: "",
    inputCompanyInfo: false,
    userInfo: '',
    isEdit:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    this.setData({
      userInfo: app.globalData.userInfo,
    })
    companyCode = app.globalData.userInfo.CompanyCode;
    realName = app.globalData.userInfo.Name;
    phoneNum = app.globalData.userInfo.Tel;
    companyName = app.globalData.userInfo.CompanyName;
    companyAddress = app.globalData.userInfo.CompanyAddress;
    storeId = app.globalData.userInfo.StoreId;
;
    companyPosition = app.globalData.userInfo.Job;
    email = app.globalData.userInfo.Email;

    if (app.globalData.userInfo.StoreId == null || app.globalData.userInfo.StoreId == undefined || app.globalData.userInfo.StoreId == ""){
      that.setData({
        isEdit: false
      })
    } else {
      that.setData({
        isEdit: true
      })
    }

  },

  inputChange: function() {
    let that = this;
    if (companyCode != null && companyCode != undefined && companyCode != '') {
      if (changCompanyCode) {
        wx.showModal({
          title: '提示',
          content: '是否绑定code为' + companyCode + '的公司',
          success(res) {
            if (res.confirm) {

              that.requestCompanyInfoForCode();
              changCompanyCode = false;

            } else if (res.cancel) {
              that.setData({
                isEdit: false
              });
              changCompanyCode = false;
            }
          }
        })
       
      }
    } else {
      that.setData({
        isEdit: false
      })
    }

  },



  register: function() {
    wx.navigateTo({
      url: "/pages/web_view/web_view?url=http://hanjiafushi.com:81/dist/regist.html"
    });
  },

  login: function() {
    wx.navigateTo({
      url: "/pages/web_view/web_view?url=http://hanjiafushi.com:81/dist/login.html"
    });
  },


  bindRealName: function(e) {
    console.log(e)
    realName = e.detail.value;
  },

  bindPhoneNum: function(e) {
    phoneNum = e.detail.value;
  },

  bindCompanyCode: function(e) {
    console.log(e.detail.value)
    let newCompanyCode = e.detail.value;
    if (newCompanyCode == companyCode) {
      changCompanyCode = false;
    } else {
      changCompanyCode = true;
      companyCode = newCompanyCode;
    }

  },

  bindCompanyName: function(e) {
    companyName = e.detail.value;
  },

  bindCompanyAddr: function(e) {
    companyAddress = e.detail.value;
  },

  bindCompanyPosition: function(e) {
    companyPosition = e.detail.value;
  },

  bindCompanyEmail: function(e) {
    email = e.detail.value;
  },

  requestCompanyInfoForCode: function() {
    var that = this;
    wx.request({
      url: app.globalData.url + "GetCompanyByCode",
      header: {
        'content-type': 'application/x-www-form-urlencoded' //默认值
      },
      data: {
        "CompanyCode": companyCode,
      },
      method: 'GET',
      success: function(res) {
        console.log(res.data)
        if (res.data.Code == 0) {
          wx.showToast({
            title: res.data.Msg,
            icon: 'none',
            duration: 5000
          })
          storeId = '';
        } else if (res.data.Code == 1) {
          companyName = res.data.ResultObject.CompanyName,
          companyAddress = res.data.ResultObject.CompanyAddress,
          storeId = res.data.ResultObject.StoreId

          that.setData({
            "userInfo.CompanyName": res.data.ResultObject.CompanyName,
            "userInfo.CompanyAddress": res.data.ResultObject.CompanyAddress
          })
        }
      }
    })
  },

  clilckShowToast: function(msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 5000
    })
    return
  },

  bindCompanyInfo: function() {
    console.log(changCompanyCode)
    if (changCompanyCode){
      return;
    }
    var that = this;
    if (realName == null || realName == undefined || realName == '') {
      that.clilckShowToast('请填写真实姓名！');
    } else if (phoneNum == null || phoneNum == undefined || phoneNum == '') {
      that.clilckShowToast('请填写手机号码！');
    } else if (companyName == null || companyName == undefined || companyName == '') {
      that.clilckShowToast('请填写公司名称！');
    } else if (companyAddress == null || companyAddress == undefined || companyAddress == '') {
      that.clilckShowToast('请填写公司地址！');
    } else if (companyPosition == null || companyPosition == undefined || companyPosition == '') {
      that.clilckShowToast('请填写公司职位！');
    } else if (email == null || email == undefined || email == '') {
      that.clilckShowToast('请填写电子邮箱！');
    } else {
      let newStoreId ='';
      if (storeId != null && storeId != undefined){
         newStoreId = storeId;
        }

      wx.request({
        url: app.globalData.url + "SaveUserInfo",
        header: {
          'content-type': 'application/x-www-form-urlencoded' //默认值
        },
        data: {
          "UserId": app.globalData.userInfo.Id,
          "Name": realName,
          "Job": companyPosition,
          "Tel": phoneNum,
          "Email": email,
          "StoreId": newStoreId,
          "CompanyAddress": companyAddress,
          "CompanyName": companyName
        },
        method: 'GET',
        success: function(res) {
          console.log(res.data)
          if (res.data.Code == 0) {
            that.clilckShowToast('保存失败！')
          } else if (res.data.Code == 1) {

            app.globalData.userInfo.Name = realName;
            app.globalData.userInfo.Tel = phoneNum;
            app.globalData.userInfo.Job = companyPosition;
            app.globalData.userInfo.CompanyName  = companyName
            app.globalData.userInfo.CompanyAddress = companyAddress
            app.globalData.userInfo.Email = email;
            app.globalData.userInfo.StoreId = newStoreId;
            app.globalData.userInfo.CompanyCode = companyCode;
            
            console.log('newStoreId');
            console.log(newStoreId);
            if (newStoreId == ''){
              that.clilckShowToast('保存成功！')
            } else {
              that.clilckShowToast('已经提交审核，请联系本公司管理员，以通过审核！')
            }
          }
        }
      })
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})